window.onload = function () {
  //获取dom元素
  const phoneH3 = document.querySelector(".phone h3");
  const phoneH4 = document.querySelector(".phone h4");
  const exitLogin = document.querySelector(".exitLogin");
  const account = document.querySelector(".account");
  const integral = document.querySelector(".integral");
  const coupons = document.querySelector(".coupons");
  const phone = document.querySelector(".phone");

  const userMessage = JSON.parse(localStorage.getItem("userMsg")) || [];
  console.log(userMessage.token);
  if (userMessage.token) {
    phone.value = userMessage.userPhone;
    phoneH3.innerHTML = filterPhoneNumber(phone.value);
    phoneH4.innerHTML = filterPhoneNumber(phone.value);
    
  }else{
    noLogin();
    exitLogin.style.display = "none";
  }

  //将获取到的号码第四位开始后的4个数字以省略号（*）替代
  //封装函数
  function filterPhoneNumber(phoneNumber) {
    let reg = /^1[3456789]{1}\d{9}$/; //必须是以1开头，第二位必须是3-9中的任意一个数，后面9位必须是数字
    //校验手机号是否正确
    if (reg.test(phoneNumber)) {
      phoneNumber = phoneNumber.toString(); //先强制转换成字符串类型
      return phoneNumber.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2");
    } else {
      return "";
    }
  }

  //点击登录
  phone.addEventListener("click", function () {
    location.href = "../login.html";
  });

  //点击退出登录
  exitLogin.addEventListener("click", function () {
    let res = confirm("您确定要退出登录吗？");
    if (res) {
      noLogin();
    }
  });

  //封装未登录状态函数
  function noLogin() {
    console.log(12)
    phoneH3.innerHTML = "未登录";
    phoneH4.innerHTML = "点击登录账号";
    let moneyStatus = "--";
    account.innerHTML = moneyStatus;
    integral.innerHTML = moneyStatus;
    coupons.innerHTML = moneyStatus;
  }
};
